package me.ak.gui;

import java.util.ArrayList;
import java.util.List;

import de.matthiasmann.twl.DialogLayout;
import de.matthiasmann.twl.Widget;

public class ToolbarWidget extends Widget{
	
	public enum Type {
		HORIZONTAL, VERTICAL
	}
	
	private Type type;
	
	public void setType(Type t) {
		type = t;
		reDesign();
	}
	
	public void addWidget(Widget w) {
		if (widgetsToAdd.contains(w)) {
			widgetsToAdd.remove(w);
		}
		widgetsToAdd.add(w);
		reDesign();
	}
	
	public void addWidgets(Widget... ws) {
		for (Widget w : ws) {
			if (widgetsToAdd.contains(w)) {
				widgetsToAdd.remove(w);
			}
			widgetsToAdd.add(w);
		}
		reDesign();
	}
	
	public Type getType() {
		return type;
	}
	
	
	private DialogLayout toolbar;
	private List<Widget> widgetsToAdd = new ArrayList<Widget>();
	
	
	public ToolbarWidget() {
		this.setTheme("");
		
		toolbar = new DialogLayout();
		toolbar.setTheme("panel");
	}
	
	private void reDesign() {
		Widget[] wa = new Widget[widgetsToAdd.size()];
		for (int i = 0; i < widgetsToAdd.size(); i ++) {
			wa[i] = widgetsToAdd.get(i);
		}
		
		if (this.type == Type.HORIZONTAL) {
			DialogLayout.Group toolbarGroup = toolbar.createSequentialGroup(wa);
			DialogLayout.Group vertg = toolbar.createParallelGroup(wa);
			toolbar.setHorizontalGroup(toolbarGroup);
			toolbar.setVerticalGroup(vertg);
		}else {
			DialogLayout.Group toolbarGroup = toolbar.createParallelGroup(wa);
			DialogLayout.Group vertg = toolbar.createSequentialGroup(wa);
			toolbar.setHorizontalGroup(toolbarGroup);
			toolbar.setVerticalGroup(vertg);
		}
		
		this.removeChild(toolbar);
		
		this.add(toolbar);
	}
	
	@Override
	protected void layout() {
		toolbar.adjustSize();
		
		this.adjustSize();
	}
	
	
	
	

}
